package com.liwei.validation;

import com.liwei.beans.Stu;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class StuValidation implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return Stu.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Stu temp=(Stu) target;
        if (temp.getId()>1000||temp.getId()<1){
            errors.rejectValue("id","stu.id","默认的错误信息，id超标");
        }

        //ValidationUtils.rejectIfEmptyOrWhitespace(errors,"name","stu.name","默认的错误信息，name不能为空");

        if (temp.getName()==null||temp.getName().trim().isEmpty()){
            errors.rejectValue("name","stu.name","默认的错误信息，name不能为空");
        }

        if (temp.getScore()>100||temp.getScore()<0){
            errors.rejectValue("score","stu.score","默认的错误信息，score超标");
        }
    }
}
